public class Test {
}
 class Stack {
    private int[] array;
    private int top;
    private int capacity;

    // 初始化栈
    public Stack(int size) {
        array = new int[size];
        capacity = size;
        top = -1;
    }

    // 入栈操作
    public void push(int value) {
        if (isFull()) {
            System.out.println("栈已满，无法入栈");
            return;
        }
        array[++top] = value;
    }

    // 出栈操作
    public int pop() {
        if (isEmpty()) {
            System.out.println("栈为空，无法出栈");
            return -1;
        }
        return array[top--];
    }

    // 获取栈顶元素
    public int peek() {
        if (isEmpty()) {
            System.out.println("栈为空");
            return -1;
        }
        return array[top];
    }

    // 判断栈是否为空
    public boolean isEmpty() {
        return top == -1;
    }

    // 判断栈是否已满
    public boolean isFull() {
        return top == capacity - 1;
    }

    // 获取栈的大小
    public int size() {
        return top + 1;
    }

    // 打印栈内元素
    public void printStack() {
        if (isEmpty()) {
            System.out.println("栈为空");
            return;
        }
        System.out.print("栈内元素: ");
        for (int i = 0; i <= top; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Stack stack = new Stack(5);

        stack.push(10);
        stack.push(20);
        stack.push(30);
        stack.printStack(); // 输出: 栈内元素: 10 20 30

        System.out.println("出栈元素: " + stack.pop()); // 输出: 出栈元素: 30
        System.out.println("栈顶元素: " + stack.peek()); // 输出: 栈顶元素: 20

        stack.printStack(); // 输出: 栈内元素: 10 20
    }
}